﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Reflection;

namespace mrp16062010.Serialize
{
    class InfoCopier
    {
        public static SerializationInfo Read(SerializationInfo info, object gelenObje)
        {
            var type = gelenObje.GetType();
            foreach (var field in type.GetFields(BindingFlags.Public |
                         BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (!field.IsNotSerialized)
                {
                    object val = info.GetValue(field.Name, field.FieldType);
                    field.SetValue(gelenObje, val);
                }
            }
            return info;
        }

        public static SerializationInfo Write(SerializationInfo info, object gelenObje)
        {
            var type = gelenObje.GetType();
            foreach (var field in type.GetFields(BindingFlags.Public |
                         BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if(!field.IsNotSerialized)
                //if (field.IsDefined(typeof(ObsoleteAttribute), true))
                {
                    object val = field.GetValue(gelenObje);
                    info.AddValue(field.Name, val);
                }

            }
            return info;
        }
    }
}
